package com.zmn.plat.admin.controller.product.tariff.item.city.price;

import java.util.*;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.common.constant.BaseAreaConsts;
import com.zmn.plat.model.entity.area.AreaQuery;
import com.zmn.plat.model.entity.area.BaseArea;
import com.zmn.plat.model.entity.product.ServProduct;
import com.zmn.plat.model.entity.product.tariff.item.city.price.ProductTariffItemCityPrice;
import com.zmn.plat.model.entity.product.tariff.item.city.price.ProductTariffItemCityPriceQuery;
import com.zmn.plat.model.entity.service.item.ServiceItem;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.entity.service.tariff.citygroup.ServiceTariffCityGroupQuery;
import com.zmn.plat.model.vo.product.tariff.item.city.price.ProductTariffItemCityPriceVo;
import com.zmn.plat.model.vo.service.tariff.citygroup.ServiceTariffCityGroupVO;

/**
 * 城市价格
 *
 * @author linfeng
 * @since 2020-03-03 10:10:14
 */
@Controller
@RequestMapping("/product/tariff/item/city/price/")
public class ProductTariffItemCityPriceListController extends ProductTariffItemCityPriceBaseController {
    private static final int DEFAULT_PAGESIZE = 100000;

    /**
     * 列表
     *
     * @param query
     * @author linfeng
     * @since 2020-03-03 10:10:14
     */
    @RequestMapping("list")
    public ModelAndView list(@ModelAttribute ProductTariffItemCityPriceQuery query) {

        List<ProductTariffItemCityPrice> itemList = productFaultItemCityPriceService.listPageByQuery(query);

        List<ProductTariffItemCityPrice> cityPriceList = productFaultItemCityPriceService.listPrice(query);

        List<Integer> priceList = cityPriceList.stream().map(ProductTariffItemCityPrice::getPrice).filter(Objects::nonNull).distinct()
            .collect(Collectors.toList());

        List<ProductTariffItemCityPriceVo> items = new ArrayList<>(itemList.size());
        itemList.forEach(e -> {
            ProductTariffItemCityPriceVo cityPriceVo = new ProductTariffItemCityPriceVo();
            BeanUtils.copyProperties(e, cityPriceVo);
            cityPriceVo.setExternalSettlementPriceYuan(narrow(cityPriceVo.getExternalSettlementPrice()));
            cityPriceVo.setInternalSettlementPriceYuan(narrow(cityPriceVo.getInternalSettlementPrice()));
            cityPriceVo.setPriceYuan(narrow(cityPriceVo.getPrice()));
            cityPriceVo.setNegotiableMaxPriceYuan(narrow(cityPriceVo.getNegotiableMaxPrice()));
            cityPriceVo.setNegotiableMinPriceYuan(narrow(cityPriceVo.getNegotiableMinPrice()));
            items.add(cityPriceVo);
        });
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        ServProduct product = servProductService.findByKey(query.getProductId());
        ServiceItem faultItem = faultItemService.findByKey(query.getItemId());
        ModelAndView mav = new ModelAndView("product/service/item/city/price/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("faultTariff", faultTariff == null ? new ServiceTariff() : faultTariff);
        mav.addObject("product", product == null ? new ServProduct() : product);
        mav.addObject("faultItem", faultItem == null ? new ServiceItem() : faultItem);
        mav.addObject("provinceList", baseAreaService.listByParentId(GlobalConsts.TOP_ID));
        mav.addObject("cityList", baseAreaService.listByParentId(query.getProvinceId()));
        mav.addObject("priceList", priceList);
        return mav;
    }

    /**
     * 详情
     *
     * @param relateId
     * @author linfeng
     * @since 2020-03-12 10:23:38
     */
    @RequestMapping("detail")
    public ModelAndView detail(@RequestParam(value = "relateId", defaultValue = "0") Integer relateId) {
        ProductTariffItemCityPrice productFaultItemCityPrice = productFaultItemCityPriceService.findByKey(relateId);
        ServiceTariff faultTariff = faultTariffService.findByKey(productFaultItemCityPrice.getTariffId());
        ProductTariffItemCityPriceVo productFaultItemCityPriceVo = new ProductTariffItemCityPriceVo();
        productFaultItemCityPriceVo = transEntityToVo(productFaultItemCityPrice);
        ServProduct product = servProductService.findByKey(productFaultItemCityPrice.getProductId());
        ServiceItem faultItem = faultItemService.findByKey(productFaultItemCityPrice.getItemId());

        ModelAndView mav = new ModelAndView("product/service/item/city/price/detail");
        mav.addObject("productFaultItemCityPrice", productFaultItemCityPriceVo);
        mav.addObject("faultTariff", faultTariff == null ? new ServiceTariff() : faultTariff);
        mav.addObject("product", product == null ? new ServProduct() : product);
        mav.addObject("faultItem", faultItem == null ? new ServiceItem() : faultItem);
        return mav;
    }

    /**
     * 城市列表
     */
    @RequestMapping(value = "citys")
    public ModelAndView listCity(ProductTariffItemCityPriceQuery query) {
        ModelAndView mav = new ModelAndView("product/service/item/city/price/citys");
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        ServProduct product = servProductService.findByKey(query.getProductId());
        ServiceItem faultItem = faultItemService.findByKey(query.getItemId());
        // 1.加载 省--》城市
        List<BaseArea> provinces = baseAreaService.listByParentId(GlobalConsts.TOP_ID);
        // 加载城市
        AreaQuery cityQuery = new AreaQuery();
        cityQuery.setLevel(BaseAreaConsts.AREA_LEVEL_THREE);
        List<BaseArea> citys = baseAreaService.listByQuery(cityQuery);
        Map<Integer, List<BaseArea>> cityMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(citys)) {
            cityMap = citys.stream().collect(Collectors.groupingBy(BaseArea::getParentId));
        }
        // 2.选中已配置的城市

        List<ProductTariffItemCityPrice> list = productFaultItemCityPriceService.listByQuery(query);
        Map<Integer, Boolean> selectMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(e -> selectMap.put(e.getCityId(), true));
        }

        if (query.getCityId() != null && list != null && list.size() > 0) {
            mav.addObject("currentCityPrice", list.get(0));
        } else {
            mav.addObject("currentCityPrice", new ProductTariffItemCityPrice());
        }
        mav.addObject("provinces", provinces);
        mav.addObject("faultTariff", faultTariff == null ? new ServiceTariff() : faultTariff);
        mav.addObject("product", product == null ? new ServProduct() : product);
        mav.addObject("faultItem", faultItem == null ? new ServiceItem() : faultItem);
        mav.addObject("cityMap", cityMap);
        // mav.addObject("selectMap",selectMap);
        mav.addObject("vo", query);

        return mav;
    }

    /**
     * 快速设置
     */
    @RequestMapping(value = "cityGroup")
    @RequiresPermissions("/product/tariff/item/city/price/cityGroup.action")
    public ModelAndView listCityGroup(ProductTariffItemCityPriceQuery query) {
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        ServProduct product = servProductService.findByKey(query.getProductId());
        ServiceItem faultItem = faultItemService.findByKey(query.getItemId());
        ModelAndView mav = new ModelAndView("product/service/item/city/price/citygroup/list");
        // 分组
        ServiceTariffCityGroupQuery cityGroupQuery = new ServiceTariffCityGroupQuery();
        cityGroupQuery.setCategOneId(product.getCategOneId());
        cityGroupQuery.setServCategId(product.getServCategId());
        cityGroupQuery.setTariffId(query.getTariffId());
        cityGroupQuery.setPageSize(DEFAULT_PAGESIZE);
        List<ServiceTariffCityGroupVO> groupList = faultCityGroupBService.listPageQuery(cityGroupQuery);
        mav.addObject("faultTariff", faultTariff == null ? new ServiceTariff() : faultTariff);
        mav.addObject("product", product == null ? new ServProduct() : product);
        mav.addObject("faultItem", faultItem == null ? new ServiceItem() : faultItem);
        mav.addObject("vo", query);
        mav.addObject("items", groupList);
        return mav;
    }

}
